gh-150942: Optimize PicklerMemoProxy.copy() with _PyDict_SetItem_Take2#151608
Merged
Conversation
Member
Author
|
cc @eendebakpt 1-2% consistenatly faster with simple replace logic, I think that it's worth to do this. import io
import pickle
import pyperf
def make_proxy(n):
buf = io.BytesIO()
pk = pickle.Pickler(buf, protocol=pickle.HIGHEST_PROTOCOL)
data = [object() for _ in range(n)]
nested = {i: [object(), object()] for i in range(n // 2)}
pk.dump(data)
pk.dump(nested)
proxy = pk.memo # PicklerMemoProxy
# Keep a reference to the source pickler alive via closure.
return pk, proxy
def main():
runner = pyperf.Runner()
for n in (1000, 20000):
pk, proxy = make_proxy(n)
entries = len(proxy.copy())
runner.bench_func(
f"PicklerMemoProxy.copy/{entries}entries",
proxy.copy,
)
if __name__ == "__main__":
main() |
Contributor
|
The change itself is ok (the dict is fresh, so no risk of concurrent threads mutating). But is the proxy used in any computations? I thought the proxy was only for human inspection. |
Member
Author
|
Yeah, I also not sure how it will be practical |
Contributor
I am fine with merging though. The code looks cleaner (although this is subjective), and if we do not merge we risk someone else making a similar PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.